package com.jeomo.base.provider.handler;

import com.jeomo.common.core.result.CommonResult;
import com.jeomo.common.core.result.ResultCode;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author:Jackpot
 * @create 2020-08-11 15:57
 * @description:
 */
@RestController
@ControllerAdvice
public class ShiroExceptionHandler {

    private Logger log = LoggerFactory.getLogger(ShiroExceptionHandler.class);

    @ExceptionHandler
    protected CommonResult handleIncorrectCredentialsException(IncorrectCredentialsException e, HttpServletRequest request) {
        log.info("handleIncorrectCredentialsException start, uri:{}, caused by: ", request.getRequestURI(), e);
        return CommonResult.failure(ResultCode.UNAUTHORIZED.code(), ResultCode.UNAUTHORIZED.msg());
    }
    @ExceptionHandler
    protected CommonResult handleUnknownAccountException(UnknownAccountException e, HttpServletRequest request) {
        log.info("handleUnknownAccountException start, uri:{}, caused by: ", request.getRequestURI(), e);
        return CommonResult.failure(ResultCode.UNAUTHORIZED.code(), ResultCode.UNAUTHORIZED.msg());
    }
    @ExceptionHandler
    protected CommonResult handleUnauthenticatedException(UnauthenticatedException e, HttpServletRequest request) {
        log.info("handleUnauthenticatedException start, uri:{}, caused by: ", request.getRequestURI(), e);
        return CommonResult.failure(ResultCode.UNAUTHORIZED.code(), ResultCode.UNAUTHORIZED.msg());
    }
    @ExceptionHandler
    protected CommonResult handleUnauthenticatedException(UnauthorizedException e, HttpServletRequest request) {
        log.info("handleUnauthenticatedException start, uri:{}, caused by: ", request.getRequestURI(), e);
        return CommonResult.failure(ResultCode.UNAUTHORIZED.code(), ResultCode.UNAUTHORIZED.msg());
    }

}
